<?php

namespace common\modules\pms\models\backend;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use yii\helpers\Html;
use yii\helpers\Url;

/**
 * ProjectSearch represents the model behind the search form about `common\modules\pms\models\backend\Project`.
 */
class ProjectSearch extends Project
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'start_time', 'end_time'], 'integer'],
            [['name', 'alias', 'description', 'start_time', 'end_time'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * @return string
     */
    public function classLabelName()
    {
        return Yii::t('backend/pms', 'Project List');
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Project::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if ( ! $this->validate())
        {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id'         => $this->id,
            'alias'      => $this->alias,
            'start_time' => $this->start_time,
            'end_time'   => $this->end_time,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'created_by' => $this->created_by,
            'updated_by' => $this->updated_by,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'description', $this->description]);

        return $dataProvider;
    }

    /**
     * 返回字段的定义信息
     *
     * @return array
     */
    public function gridViewColumns()
    {
        return [
            [
                'attribute'     => 'id',
                'filterOptions' => [
                    'style' => 'width:90px',
                ],
            ],

            'name',

            'alias',

            [
                'attribute' => 'start_time',
                'format'    => ['date', 'php:Y-m-d H:i'],
            ],

            [
                'attribute' => 'end_time',
                'format'    => ['date', 'php:Y-m-d H:i'],
            ],

            [
                'class'    => 'common\widgets\grid\ActionColumn',
                'template' => '{task} {update} {delete}',
                'buttons'  => [
                    'task' => function ($url, $model, $key)
                    {
                        return Html::a('<i class="fa fa-calendar"></i>', Url::toRoute(['/pms/task',
                            'project_id' => $model->id]));
                    },
                ],
                'width'    => '100',
            ],
        ];
    }
}
